import time
import json
import pymongo
import requests
from pyquery import PyQuery as pq
headers = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62",
}
url = "https://m.weibo.cn/api/container/getIndex?containerid=102803&openApp=0&since_id={}"
s = requests.Session()
client = pymongo.MongoClient("mongodb://localhost:27017")
db = client.Advanced
collection = db.Weibo
for i in range(1, 101):
    x = url.format(i)
    r = s.get(x, headers=headers)
    doc = pq(r.text)
    js = doc("pre").text()
    data = dict(js)
    for j in data:
        url = j.get("scheme")
        j = j.get("mblog")
        attitudes_count = j.get("attitudes_count")
        original_pic = j.get("original_pic")
        created_at = j.get("created_at")
        data = {
            "url" : url,
            "attitudes_count" : attitudes_count,
            "original_pic" : original_pic,
            "created_at" : created_at,
        }
        collection.insert_one(data)

